import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtNotifyMessageApi } from '#/api/cmt/notify/message';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入社区ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入商户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'userId',
      label: '接收会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入接收会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'messageType',
      label: '消息类型',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择消息类型',
        options: getDictOptions(DICT_TYPE.CMT_MESSAGE_TYPE, 'number'),
      },
    },
    {
      fieldName: 'title',
      label: '标题',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入标题',
      },
    },
    {
      fieldName: 'content',
      label: '内容',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 4,
        placeholder: '请输入内容',
      },
    },
    {
      fieldName: 'linkType',
      label: '链接类型',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入链接类型',
      },
    },
    {
      fieldName: 'linkId',
      label: '链接ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入链接ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'isRead',
      label: '是否已读',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: getDictOptions(DICT_TYPE.CMT_YES_NO, 'number'),
      },
      defaultValue: 0,
    },
    {
      fieldName: 'readTime',
      label: '阅读时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择阅读时间',
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        class: '!w-full',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入商户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'userId',
      label: '接收会员ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入接收会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'messageType',
      label: '消息类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择消息类型',
        options: getDictOptions(DICT_TYPE.CMT_MESSAGE_TYPE, 'number'),
      },
    },
    {
      fieldName: 'isRead',
      label: '是否已读',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择是否已读',
        options: getDictOptions(DICT_TYPE.CMT_YES_NO, 'number'),
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtNotifyMessageApi.NotifyMessage>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 80,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 100,
    },
    {
      field: 'merchantId',
      title: '商户ID',
      minWidth: 100,
    },
    {
      field: 'userId',
      title: '接收会员ID',
      minWidth: 120,
    },
    {
      field: 'messageType',
      title: '消息类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_MESSAGE_TYPE },
      },
    },
    {
      field: 'title',
      title: '标题',
      minWidth: 150,
    },
    {
      field: 'content',
      title: '内容',
      minWidth: 200,
    },
    {
      field: 'linkType',
      title: '链接类型',
      minWidth: 100,
    },
    {
      field: 'linkId',
      title: '链接ID',
      minWidth: 100,
    },
    {
      field: 'isRead',
      title: '是否已读',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_YES_NO },
      },
    },
    {
      field: 'readTime',
      title: '阅读时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
